package com.yanqu.road.server.manger;

import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.servercenter.ChannelSystemConfig;
import com.yanqu.road.entity.servercenter.SystemConfigChannel;
import com.yanqu.road.entity.systemloop.SystemLoopData;
import com.yanqu.road.logic.redis.RedisKeyDefineHelper;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SystemLoopMgr extends TempMgr {
    //key:channelId
    private static Map<Integer, Map<Integer, SystemLoopData>> loopDataMap = new HashMap<>();

    @Override
    public boolean init() throws Exception {
        return reloadConfig() && reloadData();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }


    public static void reloadDataFromRedis(int channelId) {
        getLogger().info("SystemLoopMgr reloadDataFromRedis start. channelId:{}", channelId);
        Map<Integer, SystemLoopData> tempDataMap = RedisManger.getRedisUtil().getObject(RedisKeyDefineHelper.getChannelSystemLoopKey(channelId), new TypeReference<Map<Integer, SystemLoopData>>() {
        });
        if (tempDataMap == null) {
            return;
        }

        loopDataMap.put(channelId, tempDataMap);

        getLogger().info("SystemLoopMgr reloadDataFromRedis end. channelId:{}", channelId);
    }

    public static List<Integer> getNowWeekSystemIds(int channelId, long serverId) {
        Map<Integer, SystemLoopData> dataMap = loopDataMap.get(channelId);
        if (dataMap == null) {
            return new ArrayList<>();
        }
        List<Integer> systemIdList = new ArrayList<>();
        for (SystemLoopData loopData : dataMap.values()) {
            if (loopData.getServerIdSet().contains(serverId)) {
                systemIdList.add(loopData.getSystemId());
            }
        }

        return systemIdList;
    }


}
